package com.csdj.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.csdj.common.enums.AppHttpCodeEnum;
import com.csdj.common.utils.ResponseData;
import com.csdj.common.utils.ResponseResult;
import com.csdj.pojo.*;
import com.csdj.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author: DengYuChang
 * @date: 2023-08-29 9:50
 * @description:
 */

@RestController
@RequestMapping("ivArchive")
public class IvArchiveController {

    @Autowired
    private IvArchiveService ivArchiveService;

    @Autowired
    private IvOutinvoiceService ivOutinvoiceService;

    @Autowired
    private IvInvoiceService ivInvoiceService;

    @Autowired
    private SyEmpService syEmpService;

    @Autowired
    private IvIninvoiceService ininvoiceService;


    @PostMapping("isAccordWith")
    public ResponseResult isAccordWith(@RequestBody IvArchive ivArchive) {
        int startNo = Integer.parseInt(ivArchive.getStartNo());
        int endNo = Integer.parseInt(ivArchive.getEndNo());
        int ivCount = ivArchiveService.countByStartNoAndEndNo(startNo, endNo);
        if (ivCount != 0) {
            return ResponseResult.errorResult(400, "该范围内已有发票交票,不可重复交票,请重新输入");
        }
        int countByStartNoAndEndNo = ivOutinvoiceService.getCountByStartNoAndEndNo(startNo, endNo);
        if (countByStartNoAndEndNo != 1) {
            return ResponseResult.errorResult(400, "该区间内有发票不在交票范围内,请检查后重新输入");
        }
        IvOutinvoice ivOutinvoice = ivOutinvoiceService.getOneByByStartNoAndEndNo(startNo, endNo);
        if(Integer.parseInt(ivOutinvoice.getStartNo())>startNo || Integer.parseInt(ivOutinvoice.getEndNo())<endNo){
            return ResponseResult.errorResult(400, "该区间内有发票不在交票范围内,请检查后重新输入");
        }
        int countByStartNoAndEndNoAndUserd = ivInvoiceService.getCountByStartNoAndEndNoAndUserd(startNo, endNo);
        if (countByStartNoAndEndNoAndUserd>0){
            return ResponseResult.errorResult(400, "该区间内有发票待使用,请检查后重新输入");
        }

        Integer empID = ivOutinvoice.getEmpID();
        SyEmp syEmp = syEmpService.getById(empID);
        String type;
        IvIninvoice ininvoice = ininvoiceService.getOneBystartNoAndendNo(startNo, endNo);
        type=ininvoice.getInvoiceType()==0?"普通发票":"增值税发票";
        Map<Object,Object> map=new HashMap<>();
        map.put("empID",syEmp.getId());
        map.put("empName",syEmp.getEmpName());
        map.put("type",type);
        map.put("InID",ininvoice.getID());
        return ResponseResult.okResult(map);
    }

    @PostMapping("/add")
    public ResponseResult add(@RequestBody IvArchive ivArchive){
        boolean save = ivArchiveService.save(ivArchive);
        boolean b = ivInvoiceService.UpdateArchive(ivArchive);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @GetMapping("listmap")
    public ResponseData listMap(@RequestParam(defaultValue="1" ) int PageNum){
        Page<Map> page=new Page<>(PageNum,2);
        Page<Map> listMap = ivArchiveService.listmap(page);
        return ResponseData.ok().putDataValue("page",listMap);
    }
}
